package com.jvm123.blog.controller

import com.jvm123.blog.service.CollectService
import com.jvm123.blog.service.MenuService
import com.jvm123.blog.service.PostService
import com.jvm123.blog.service.TermService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable

/**
 *
 * @author yawn http://jvm123.com
 *     2019/10/24 15:04
 */
@Controller
class PostController {

    @Autowired
    MenuService menuService
    @Autowired
    PostService postService
    @Autowired
    CollectService collectService
    @Autowired
    TermService termService

    @GetMapping(["post/{id}/{title}", "post/{id}"])
    String getOne(@PathVariable("id") long id,
                  @PathVariable(value = "title", required = false) String title,
                  Model model) {
        model.addAttribute("topMenus", menuService.getTopMenu())
        model.addAttribute("post", postService.findPostWithTags(id))
        "postDetail"
    }

    @GetMapping(["app/post/{id}/{title}", "app/post/{id}"])
    String appGetOne(@PathVariable("id") long id,
                  @PathVariable(value = "title", required = false) String title,
                  Model model) {
        model.addAttribute("post", postService.findPostWithTags(id))
        "postAppDetail"
    }

    @GetMapping(["tag/{tag}", "tag/{tag}/{name}"])
    String tagPosts(@PathVariable("tag") String tag,
                    @PathVariable(value = "name", required = false) String name,
                    Model model) {
        model.addAttribute("topMenus", menuService.getTopMenu())
        model.addAttribute("term", termService.findBySlug(tag))
        model.addAttribute("posts", postService.findByTag(tag))
        "posts"
    }

    @GetMapping(["category/{category}", "category/{category}/{name}"])
    String catPosts(@PathVariable("category") String category,
                    @PathVariable(value = "name", required = false) String name,
                    Model model) {
        model.addAttribute("topMenus", menuService.getTopMenu())
        model.addAttribute("term", termService.findBySlug(category))
        model.addAttribute("posts", postService.findByCategory(category))
        "posts"
    }

}
